package com.pet.controller;

import com.pet.pojo.SysSmallDog;
import com.pet.pojo.Result;
import com.pet.service.ISysSmallDogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/small-dog")
public class SysSmallDogController {

    @Autowired
    private ISysSmallDogService smallDogService;

    @GetMapping("/list")
    public Result list(
            @RequestParam(required = false) String breed,
            @RequestParam(required = false) String age,
            @RequestParam(required = false) String gender,
            @RequestParam(required = false) String priceRange,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "8") Integer pageSize) {

        Map<String, Object> params = new HashMap<>();
        params.put("breed", breed);
        params.put("age", age);
        params.put("gender", gender);

        // 处理价格范围
        if (priceRange != null && !priceRange.isEmpty()) {
            String[] prices = priceRange.split("-");
            params.put("minPrice", Integer.parseInt(prices[0]));
            params.put("maxPrice", Integer.parseInt(prices[1]));
        }

        // 分页参数
        params.put("offset", (page - 1) * pageSize);
        params.put("limit", pageSize);

        return smallDogService.findByCondition(params);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return smallDogService.findById(id);
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysSmallDog smallDog) {
        return smallDogService.add(smallDog);
    }

    @PutMapping("/update")
    public Result update(@RequestBody SysSmallDog smallDog) {
        return smallDogService.update(smallDog);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return smallDogService.delete(id);
    }
} 